package com.asohofrucol.web;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.dto.LivestockResourceRegisterDTO;
import com.asohofrucol.service.UserOwnerService;

public class AnimalBusinessController {
	@Autowired
	private AsohofrucolSessionDTO aSession;
	@Autowired
	private UserOwnerService userOwnerService;

	public void createNewRegister(ActionEvent actionEvent) {
		if (aSession.getLivestockResource().getLivestockResourceRegisters()
				.size() < 3) {
			Long ruatId = aSession.getSelectedRuatRegister().getId();
			LivestockResourceRegisterDTO livestockResourceRegisterDTO = new LivestockResourceRegisterDTO();
			livestockResourceRegisterDTO.setRuatId(ruatId);
			livestockResourceRegisterDTO.setStatus("1");
			aSession.getLivestockResource().getLivestockResourceRegisters()
					.add(livestockResourceRegisterDTO);
		}

	}
	public void deleteRegister(ActionEvent actionEvent) {
		LivestockResourceRegisterDTO selected = (LivestockResourceRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getLivestockResource().getLivestockResourceRegisters()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteLivestockResourceRegister(selected, userModifier);
		}
	}
}
